﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace Flatland.Utilities {
    /// <summary>
    /// Utils for GP.
    /// </summary>
    public class Utils {
        private static Random _random;
        /// <summary>
        /// Get a Random object.
        /// </summary>
        public static Random Random {
            get {
                if (_random == null)
                    _random = new Random();
                return _random;
            }
        }


        /// <summary>
        /// Deletes all files from a directory
        /// </summary>
        /// <param name="directory"></param>
        private void CleanDirectory(string directory) {
            string[] filePaths = Directory.GetFiles(directory);
            foreach (string filePath in filePaths)
                File.Delete(filePath);
        }

        /// <summary>
        /// Get all system colors
        /// </summary>
        /// <returns></returns>
        public static Color[] SystemColors() {
            List<Color> predefinedColors = new List<Color>();
            PropertyInfo[] properties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
            foreach (PropertyInfo propertyInfo in properties) {
                if (propertyInfo.GetGetMethod() != null && propertyInfo.PropertyType == typeof(Color)) {
                    Color color = (Color)propertyInfo.GetValue(null, null);
                    predefinedColors.Add(color);
                }
            }
            return predefinedColors.ToArray();
        }

        /// <summary>
        /// Get a current timestamp in a string usable for folder and file names.
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp() {
            return new[] { ' ', '\\', ':', ';', '/' }.Aggregate("T" + DateTime.Now.ToString(CultureInfo.InvariantCulture),
                                                              (current, c) =>
                                                              current.Replace(c.ToString(CultureInfo.InvariantCulture), ""));
        }

        public static double PointDist(double a1, double a2, double b1, double b2) {
            var a_ = b1 - a1;
            var b_ = b2 - a2;
            return Math.Sqrt(a_ * a_ + b_ * b_);
        }

        public static double DegreeToRadian(double angle) {
            return Math.PI * angle / 180.0;
        }
    }
}
